use "$temp/nlsy_base_sample", clear

//parent income/wealth quintiles
drop if parent_inc == . | parent_house == . //require that parent income/wealth be observed
xtile parent_inc_quart = parent_income [fw = weight_1997], nq(4)
xtile parent_inc_dec = parent_income [fw = weight_1997], nq(10)
xtile parent_house_quart = parent_house [fw = weight_1997], nq(4)

//reshape to long
reshape long hgc_ enroll_ hideg_ wage_ weight_, i(uniqid) j(year)

******deflate stuff***********
merge m:1 year using "$data/GDP/gdp_pce_deflator", keep(match) nogen
replace deflator = deflator/100 //normalize
replace wage = wage/deflator //deflate labor income

****1997 deflator applied to parent information
su deflator if year == 1997
replace parent_income_1997 = parent_income/`r(mean)'
replace parent_house_value = parent_house_value/`r(mean)'
drop deflator

****normalize a few things by 10k
ds parent_house_value parent_income_1997
foreach var in `r(varlist)'{
    replace `var' = `var' / 40000
}

//generate age variable in preparation for reshaping
gen age = year - birthyr
drop year sex race 
su wage, d
sort uniqid age

//final educational attainment
sort uniqid age
replace hgc_ = hgc_[_n-1] if hgc_[_n] == . & hgc_[_n-1]!=. & uniqid[_n] == uniqid[_n-1] //fill in hgc
replace hgc_ = hgc_[_n-1] if hgc_[_n] == . & hgc_[_n-1]!=. & uniqid[_n] == uniqid[_n-1] //fill in hgc
gen temp = hgc if age >=27 & age<=29
bys uniqid: egen hgc_final = max(temp)
drop temp

//compressed version
gen hgc_final_comp = 1
replace hgc_final_comp = 2 if hgc_final == 14 | hgc_final == 15
replace hgc_final_comp = 3 if hgc_final>=16 & hgc_final!=.

*****reshape back to wide and begin generating moments
reshape wide wage_ hgc_ enroll_ hideg_ weight_, i(uniqid) j(age)
order uniqid birthyr sample ability  ///
parent_house* parent_inc* parent_net_worth hgc* enroll* hideg* ///
wage* weight*

save "$temp/nlsy_moment_data", replace





//end of dofile